import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-03-11
 * Time: 10:17
 */

public class myArrayList {
    public int[] elem;//创建一个数组
    public  static int initCapacity=10;
    public int usedSize;//数据的有效个数

    //构造一个方法
    public myArrayList(){
        this.usedSize=0;//初始化为0
        this.elem = new int[initCapacity];
    }

    private boolean isFull() {
        return this.elem.length==this.usedSize;
    }

    public void Add(int pos,int data) {
        //1.判断usedSize为有效个数不能溢出
        //2.usedSize不能为负数不能超过数组长度
        //将数据插入到pos位置上
        if(isFull()) {
            this.elem=Arrays.copyOf(this.elem,this.elem.length*2);
        }
        if(pos<0||pos> this.usedSize) {
            return ;
        }
        for (int i = this.usedSize-1; i >=pos ; i--) {
               this.elem[i+1]=this.elem[i];
        }
        //此时为pos位置
        this.elem[pos]=data;
        this.usedSize++;
    }

    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(elem[i]+" ");
           // System.out.println(Arrays.toString(elem));
        }
        System.out.println();
    }

    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i]==toFind) {
                return true;
            }
        }
        return false;
    }

    public int search(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i]==toFind) {
                return i;
            }
        }
        return -1;
    }

    private boolean isEmpty() {

        return this.usedSize==0;
    }
    public int getPos(int pos) {
        //给你一个下标返回下标的元素
        //判断数组是否为空
        if(isEmpty()) {
            throw new RuntimeException("顺序表为空");
        }
        //判断数组是否在有效范围内
        if(pos<0||pos>=usedSize) {
            return -1;
        }
        return this.elem[pos];
    }

    public  int size() {
        return usedSize;
    }

    public void remove(int toRemove) {
        //找到index  然后将它删除
        int index = search(toRemove);
        if(index ==-1) {
            System.out.println("没有要删除的数字：《》");
            return;
        }
        for(int i=index ;i<this.usedSize-1;++i) {
            this.elem[i] =this.elem[i+1];
        }
        this.usedSize--;
    }
}

